#include <string>
#include <vector>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include "time.h"
#include "split.h"

using namespace std;
using namespace stock_trade;

Time::Time(string strTime)
{
    vector<string> vecTime;
    split(strTime, ":", vecTime);
    if (vecTime.size() != 3)
        return;
    hour = atoi(vecTime[0].c_str());
    minute = atoi(vecTime[1].c_str());
    second = atoi(vecTime[2].c_str());
}

Time Time::GetNextTime()
{
    return Time(0, 0, 0);
}

bool Time::operator==(const Time& time) const
{
    return hour == time.hour && minute == time.minute && second == time.second;
}

bool Time::operator>(const Time& time) const
{
    if (hour > time.hour)
        return true;
    if (hour < time.hour)
        return false;

    if (minute > time.minute)
        return true;
    if (minute < time.minute)
        return false;

    if (second > time.second)
        return true;
    if (second < time.second)
        return false;
    return false;
}

bool Time::operator<(const Time& time) const
{
    if (!(*this == time) && !(*this > time))
        return true;
    return false;
}

bool Time::operator>=(const Time& time) const
{
    return (*this > time) || (*this == time);
}

bool Time::operator<=(const Time& time) const
{
    return (*this < time) || (*this == time);
}

int Time::operator-(const Time& time) const
{
    return (second - time.second)
         + (minute - time.minute) * 60
         + (hour - time.hour) * 60 * 60;
}

Time Time::operator++()
{
    //if (second != 59)
    //{
    //    second++;
    //    return *this;
    //}
    ////new minute
    //second = 0;

    ////if (second < 30)
    ////{
    ////    second += 30;
    ////    return *this;
    ////}
    ////second = second + 30 -60;

    //if (minute < 58)
    //{
    //    minute+=2;
    //    return *this;
    //}
    //minute = minute + 2 - 60;
    if (minute != 59)
    {
        minute++;
        return *this;
    }
    //new hour
    minute = 0;
    if (hour != 23)
    {
        hour++;
        return *this;
    }
    hour = 0;
    return *this;
}

ostream& operator<<(ostream& os, const Time& time)
{
    os << time.hour << ":" << time.minute << ":" << time.second << "\t";
    return os;
}

Time GetNow()
{
    time_t tm;
    tm = time(NULL);
    struct tm *tblock;
    tblock = localtime(&tm);
    Time time(tblock->tm_hour, tblock->tm_min, tblock->tm_sec);
    return time;
}
